package cn.xiaoyuan.controller.api;

import cn.xiaoyuan.entity.AuthUser;
import cn.xiaoyuan.entity.Product;
import cn.xiaoyuan.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/api/user")
public class UserApiController {
    @Resource
    UserService userService;
    @RequestMapping("/del_sell/{id}")
    public String del_sell(@PathVariable("id") int id, Model model){
        if(!userService.delete_sellId(id)){
            model.addAttribute("mess","在售的商品不能下架，如若下架，请先改变交易状态");
        }
        return "forward:/page/user/orders";
    }
    @RequestMapping("/add_product")
    public String add_product(@RequestParam("ptitle")String ptitle, @RequestParam("pdesc")String pdesc, HttpSession session){
        AuthUser user=(AuthUser)session.getAttribute("user");
        System.out.println(ptitle+pdesc);
        userService.insertProduct(ptitle,pdesc,user.getId());
        return "redirect:/page/user/orders";
    }
    @RequestMapping("/up_sell/${id}")
    public String up_sell(@PathVariable("id")int id,Model model){
          Product product=userService.getProductById(id);
          model.addAttribute("product",product);
          return "forward:/page/user/addProduct";
    }

}
